package com.epam.training.domain;

import java.io.Serializable;

import javax.persistence.*;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Pattern.Flag;

import java.util.List;

/**
 * The persistent class for the customer database table.
 * 
 */
@Entity
@Table(name = "customer")
@DiscriminatorValue("customer")
@PrimaryKeyJoinColumn(name = "customer_id", referencedColumnName = "user_id")
// @NamedQuery(name = "Customer.findAll", query = "SELECT c FROM Customer c")
public class Customer extends User implements Serializable {
	private static final long serialVersionUID = 1L;

	@Column(name = "customer_id", insertable = false, updatable = false, unique = true, nullable = false)
	private Integer customerId;

	@Column(name = "drive_lecence", length = 20)
	private String driveLicense;

	@Column(length = 40)
	@Pattern(regexp = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\."
			+ "[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@"
			+ "(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message = "{invalid.email}")
	private String email;

	@Column(length = 20)
	private String name;

	@Column(name = "second_name", length = 40)
	private String secondName;

	@Column(length = 40)
	private String surname;

	@Column(length = 10)
	@Pattern(regexp = "\\d{10}", message = "{invalid.number}", flags = { Flag.CASE_INSENSITIVE })
	private String telephone;

	// bi-directional many-to-one association to Bookingitem
	@OneToMany(mappedBy = "customer", orphanRemoval = true)
	private List<BookingItem> bookingItems;

	public Customer() {
	}

	public Integer getCustomerId() {
		return this.customerId;
	}

	public void setCustomerId(Integer customerId) {
		this.customerId = customerId;
	}

	public String getDriveLicense() {
		return this.driveLicense;
	}

	public void setDriveLicense(String driveLecence) {
		this.driveLicense = driveLecence;
	}

	public String getEmail() {
		return this.email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSecondName() {
		return this.secondName;
	}

	public void setSecondName(String secondName) {
		this.secondName = secondName;
	}

	public String getSurname() {
		return this.surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public String getTelephone() {
		return this.telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public List<BookingItem> getBookingItems() {
		return this.bookingItems;
	}

	public void setBookingitems(List<BookingItem> bookingItems) {
		this.bookingItems = bookingItems;
	}

	public BookingItem addBookingitem(BookingItem bookingItem) {
		getBookingItems().add(bookingItem);
		bookingItem.setCustomer(this);

		return bookingItem;
	}

	public BookingItem removeBookingitem(BookingItem bookingItem) {
		getBookingItems().remove(bookingItem);
		bookingItem.setCustomer(null);

		return bookingItem;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((bookingItems == null) ? 0 : bookingItems.hashCode());
		result = prime * result
				+ ((customerId == null) ? 0 : customerId.hashCode());
		result = prime * result
				+ ((driveLicense == null) ? 0 : driveLicense.hashCode());
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((secondName == null) ? 0 : secondName.hashCode());
		result = prime * result + ((surname == null) ? 0 : surname.hashCode());
		result = prime * result
				+ ((telephone == null) ? 0 : telephone.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		Customer other = (Customer) obj;
		if (bookingItems == null) {
			if (other.bookingItems != null)
				return false;
		} else if (!bookingItems.equals(other.bookingItems))
			return false;
		if (customerId == null) {
			if (other.customerId != null)
				return false;
		} else if (!customerId.equals(other.customerId))
			return false;
		if (driveLicense == null) {
			if (other.driveLicense != null)
				return false;
		} else if (!driveLicense.equals(other.driveLicense))
			return false;
		if (email == null) {
			if (other.email != null)
				return false;
		} else if (!email.equals(other.email))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (secondName == null) {
			if (other.secondName != null)
				return false;
		} else if (!secondName.equals(other.secondName))
			return false;
		if (surname == null) {
			if (other.surname != null)
				return false;
		} else if (!surname.equals(other.surname))
			return false;
		if (telephone == null) {
			if (other.telephone != null)
				return false;
		} else if (!telephone.equals(other.telephone))
			return false;
		return true;
	}

}
